package cn.wolfcode.web.interceptor;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequirePermission;
import cn.wolfcode.util.UserContext;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


public class CheckPermissionInterceptor implements HandlerInterceptor {
    //5.1页面拦截器查询了权限,这里就不用再查询
    /*@Autowired
    private IPermissionService permissionService;
    */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.获取当前登录的用户
        //Employee employee = (Employee) request.getSession().getAttribute("USER_IN_SESSION");
        //抽取后.注意强转
        Employee employee = UserContext.getCurrentUser();
        //2.判断是否超级管理员
        if (employee.isAdmin()){
            //是:放行
            return true;
            //否:下一步
        }
        //3.获取目标方法(想要请求的方法)
        //拦截器preHandle的第三个参数就是当前被拦截的处理方法
        HandlerMethod method = (HandlerMethod)handler;
        //4.判断是否贴自定义权限标签@RequirePermission
        RequirePermission requirePermission = method.getMethodAnnotation(RequirePermission.class);
        if (requirePermission == null){
            //否:放行
            return true;
            //是:下一步
        }
        //5.获取用户拥有的权限表达式列表
        //5.2用户拦截器里获取了用户拥有的权限表达式列表,这里就不用5.2的方法
        //List<String> expressions = permissionService.selectByEmpId(employee.getId());

        //通过用户拦截器里的结果获取,不用每次判断的时候都去查数据库
        //抽取工具后,不用这个
        //List<String> expressions = (List)request.getSession().getAttribute("EXPRESSION_IN_SESSION");
        List<String> expressions = UserContext.getExpressions();

        //6.从注解上获取当前目标方法对应的权限表达式
        String expression = requirePermission.expression();
        //7.判断该表达式是否存在用户列表中
        if (expressions.contains(expression)){
            //是:放行
            return true;
        }
        //否:返回错误提示页面,不放行(使用转发)

        //判断前段是否ajax异步请求,如果是就返回json数据(解决删除数据时提示框弹出空白)
        if (method.hasMethodAnnotation(ResponseBody.class)){
            //步骤四:设置响应头
            //法一:
            response.setContentType("application/json;charset=UTF-8");
            //法二:
            //response.addHeader("content-type","application/json;charset=UTF-8");

            //步骤一:new JsonResult(false,"没有操作权限");
            //步骤二:JSON.toJSONString(new JsonResult(false,"没有操作权限"));
            //response响应
            //步骤三
            response.getWriter().print(JSON.toJSONString(new JsonResult(false,"没有操作权限")));
        }else {
            request.getRequestDispatcher("/WEB-INF/views/common/nopermission.jsp").forward(request,response);
        }

        return false;
    }
}
